import {createStore} from 'vuex'

import { getUserFn,menuListFn } from '@/api/login'

const store = createStore({
    state () {
        return {
            menus:[],
            userInfo:{}          
        }
    },
    mutations: {   
        //修改管理员信息
        setUserInfo(state,userInfo){
            state.userInfo=userInfo.data
        },
        setMenus(state,menus){
            state.menus=menus
        },
    },
    actions:{
        getUserInfo(context){
            return new Promise((resolve,reject)=>{
                let tokenStr = window.sessionStorage.getItem('token')
                let obj = {token:tokenStr}
                getUserFn(obj).then(res=>{
                    context.commit('setUserInfo',res) 
                    resolve(res)
                }).catch(err=>reject(err))

                menuListFn(obj).then(res=>{
                    console.log(res.data.result)
                    context.commit('setMenus',res.data.result)
                    resolve(res)
                }).catch(err=>reject(err))
            })        
        }
    }
})

export default store